package com.ego.product.controller;

import java.util.Arrays;
import java.util.List;

import com.ego.common.vo.PageVo;
import com.ego.product.vo.SkuFilterConditionVo;
import com.ego.product.vo.SkuFilteredVo;
import com.ego.product.vo.SkuInfoSearchVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ego.product.po.SkuInfo;
import com.ego.product.service.SkuInfoService;
import com.ego.common.util.Result;

import javax.validation.Valid;

/**
 * @author KamTang
 * @program: ego
 * @description sku信息控制器
 * @date 2022-11-13 12:56:14
 */
@RestController
@RequestMapping("product/sku-info")
public class SkuInfoController {

    @Autowired
    private SkuInfoService skuInfoService;

    /**
     * 信息
     */
    @GetMapping("/get/{skuId}")
    public Result get(@PathVariable("skuId") Long skuId) {
        SkuInfo skuInfo = skuInfoService.getById(skuId);
        return Result.ok().put("skuInfo", skuInfo);
    }

    /**
     * 搜索
     */
    @PostMapping("/search")
    public Result search(@Valid @RequestBody SkuInfoSearchVo search) {
        PageVo<SkuInfo> page = skuInfoService.search(search);
        return Result.ok().put("page", page);
    }

    /**
     * 过滤其他sku，满足用户选择的分类下和品牌
     */
    @PostMapping("/filter")
    public Result filter(@Valid @RequestBody SkuFilterConditionVo filter) {
        List<SkuFilteredVo> list = skuInfoService.filter(filter);
        return Result.ok().put("list", list);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public Result save(@RequestBody SkuInfo skuInfo) {
        skuInfoService.save(skuInfo);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Result update(@RequestBody SkuInfo skuInfo) {
        skuInfoService.updateById(skuInfo);
        return Result.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody Long[] skuIds) {
        skuInfoService.removeByIds(Arrays.asList(skuIds));
        return Result.ok();
    }

}
